package com.okq.service.Impl;

import com.okq.dao.CustomerDao;
import com.okq.dao.UserDao;
import com.okq.entity.Customer;
import com.okq.entity.User;
import com.okq.service.ICustomerService;
import com.okq.util.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 描述 : 客户业务接口实现类
 * 作者 : zdl
 * 日期 : 2017/6/28 17:35
 */
@Transactional
@Service
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private UserDao userDao;

    @Override
    public void saveCustomer(Customer customer) {
        customerDao.saveCustomer(customer);
    }

    @Override
    public void deleteCustomer(Customer customer) {
        customerDao.deleteCustomer(customer);
    }

    @Override
    public void updateCustomer(Customer customer) {
        customerDao.updateCustomer(customer);
    }

    @Override
    public void saveOrUpdateCustomer(Customer customer) {
        customerDao.saveOrUpdateCustomer(customer);
    }

    @Override
    public Customer getCustomerById(String id) {
        return customerDao.getCustomerById(id);
    }

    @Override
    public List<Customer> listAllCustomer() {
        return customerDao.listAllCustomer();
    }

    @Override
    public Page<Customer> pageByCustomer(Page<Customer> page, Customer customer) {
        if (customer != null) {
            String userName = customer.getUserName();
            if (StringUtils.isNotBlank(userName)) {
                List<User> uList = userDao.listUserByUserName(userName);
                if (uList != null && !uList.isEmpty() && uList.size() > 0) {
                    String userIds = "";
                    for (User u : uList) {
                        userIds = u.getId() + ",";
                    }
                    if (StringUtils.isNotBlank(userIds)) {
                        userIds = userIds.substring(0, (userIds.length() - 1));
                        customer.setUserIds(userIds);
                    }
                }
            }
        }
        return customerDao.pageByCustomer(page, customer);
    }

    @Override
    public Customer getCustomerByName(String name) {
        return customerDao.getCustomerByName(name);
    }

    @Override
    public Customer getCustomerByContact(String contact) {
        return customerDao.getCustomerByContact(contact);
    }

    @Override
    public List<Customer> listCustomerByKeyword(String keyword) {
        return customerDao.listCustomerByKeyword(keyword);
    }

    @Override
    public List<Customer> listCustomerByComCode(String comCode) {
        return customerDao.listCustomerByComCode(comCode);
    }

	@Override
	public String getDictByValueAndType(String productName, String productType) {
		// TODO Auto-generated method stub
		return null;
	}


}
